package com.yzx.lifeassistants.model.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;

import com.yzx.lifeassistants.bean.JobInfo;
import com.yzx.lifeassistants.model.IQueryJob;
import com.yzx.lifeassistants.model.callback.IQueryJobCB;
import com.yzx.lifeassistants.utils.HttpClientUtil;
import com.yzx.lifeassistants.utils.IHttpCallBack;
import com.yzx.lifeassistants.utils.LogcatUtils;
import com.yzx.lifeassistants.utils.PropertyUtil;

/**
 * @Description: 查询招聘信息
 * @author: yzx
 * @time: 2016-3-22 下午9:33:50
 */
public class QueryJobImpl implements IQueryJob {

	public static final String TAG = "QueryJobInfo";
	private String url;// URL
	private String refererUrl;// 绝对路径前缀
	private IQueryJobCB queryCallBack;// 回调

	public QueryJobImpl(Context context, IQueryJobCB queryCallBack) {
		this.queryCallBack = queryCallBack;
		Properties properties = PropertyUtil.getProperties(context,
				"base.properties");
		url = properties.getProperty("query_job_url");
		refererUrl = properties.getProperty("job_referer_url");
	}

	/**
	 * 
	 * @Description: 查询招聘信息
	 */
	@Override
	public void queryJob(int page, int type) {
		String url = this.url + "?page=" + page + "&pubtype=" + type;
		HttpClientUtil.sendRequestWithGBK(url, null, new IHttpCallBack() {

			@Override
			public void onFinish(String response) {
				LogcatUtils.i(TAG, response);
				if (null == response || "".equals(response)) {
					LogcatUtils.e("获取数据失败");
					queryCallBack.queryError(IQueryJobCB.RESULR_NULL);
				} else {
					List<String> urlList = new ArrayList<String>();
					List<String> titleList = new ArrayList<String>();
					List<String> placeList = new ArrayList<String>();
					List<String> dateList = new ArrayList<String>();
					Document doc = Jsoup.parse(response);
					Elements elements = doc.select("div#todayR");
					for (Element element : elements) {
						Elements nameElements = element
								.getElementsByAttributeValue("class",
										"todayR-name");
						for (Element nameElement : nameElements) {
							String url = nameElement.select("a").attr("href");
							urlList.add(refererUrl + url);
							String title = nameElement.select("a").text();
							titleList.add(title.substring(1));
						}
						Elements infoElements = element
								.getElementsByAttributeValue("class",
										"todayR-con");
						Boolean isDate = true;
						for (Element infoElement : infoElements) {
							String info = infoElement.text();
							if (isDate) {
								if (1 == info.substring(info.indexOf(":") + 1,
										info.length()).length()) {
									info = info + "0";
								}
								if (2 == info.substring(info.indexOf("日") + 1,
										info.indexOf(":")).length()) {
									String front = info.substring(0,
											info.indexOf("日") + 2);
									info = front
											+ "0"
											+ info.substring(
													info.indexOf(":") - 1,
													info.length());
								}
								if (1 == info.substring(info.indexOf("月") + 1,
										info.indexOf("日")).length()) {
									String front = info.substring(0,
											info.indexOf("月") + 1);
									info = front
											+ "0"
											+ info.substring(
													info.indexOf("月") + 1,
													info.length());
								}
								if (1 == info.substring(info.indexOf("年") + 1,
										info.indexOf("月")).length()) {
									String front = info.substring(0,
											info.indexOf("年") + 1);
									info = front
											+ "0"
											+ info.substring(
													info.indexOf("年") + 1,
													info.length());
								}
								dateList.add(info);
								isDate = false;
							} else {
								placeList.add(info);
								isDate = true;
							}
						}
					}
					if (haveData(urlList) && haveData(titleList)
							&& haveData(dateList) && haveData(placeList)) {
						List<JobInfo> jobInfos = new ArrayList<JobInfo>();
						for (int i = 0; i < urlList.size(); i++) {
							JobInfo jobInfo = new JobInfo();
							jobInfo.setUrl(urlList.get(i));
							jobInfo.setTitle(titleList.get(i));
							jobInfo.setDate(dateList.get(i));
							jobInfo.setPlace(placeList.get(i));
							jobInfos.add(jobInfo);
						}
						if (null == jobInfos || 1 > jobInfos.size()) {
							LogcatUtils.e("获取数据失败");
							queryCallBack.queryError(IQueryJobCB.RESULR_NULL);
						} else {
							LogcatUtils.i("获取数据成功");
							queryCallBack.querySuccess(jobInfos);
						}
					} else {
						LogcatUtils.e("获取数据失败");
						queryCallBack.queryError(IQueryJobCB.RESULR_NULL);
					}
				}
			}

			@Override
			public void onError(Exception e) {
				LogcatUtils.e("获取数据异常");
				if (null != e.getMessage()) {
					LogcatUtils.e(TAG, e.getMessage());
				}
				queryCallBack.queryError(IQueryJobCB.QUERY_ERROR);
			}
		});
	}

	/**
	 * 
	 * @Description: 是否有数据
	 */
	private Boolean haveData(List<String> urlList) {
		if (null == urlList || 1 > urlList.size()) {
			return false;
		} else {
			return true;
		}
	}
}
